//TODO Contar puntos de las cartas y controlar comodines y especiales.

package Game;

import Cards.UnoCard;
import Internationalization.LoadInternationalization;
import java.util.Collections;
import java.util.Scanner;

/**
 * Class PlayerUNO. It mages PlayerUNO
 */

public class PlayerUNO extends Player<UnoCard>{
    protected UnoGame currentGame;
    
    protected PlayerUNO(String s){
        super(s);
        currentGame = null;
    }
    
    //Método llamado por JuegoBJ cuando es tu turno. Despliega un menú de posibles movimientos
    //Método interfaz, se sobreescribirá en las clases BOT
    
     /**
     * Method to decide the turn
     * @param currentGame
     */
    
    public void myTurn(UnoGame currentGame){
        UnoCard uc = new UnoCard(UnoCard.Number.ZERO, UnoCard.Colour.RED);
        this.currentGame = currentGame;
        Scanner sc = new Scanner(System.in);
        int opt = 0;
        boolean played = false;
        if (currentGame.getTopCard().getRank().equals(UnoCard.Number.TAKETWO) && !currentGame.getRobado()){
            for (UnoCard c : hand){
                if (!played){
                    if (c.getRank().equals(UnoCard.Number.TAKETWO)){
                        while (opt != 1 && opt != 2){
                            
                            System.out.println(LoadInternationalization.getRB().getString("anotherTWO"));
                            System.out.println(LoadInternationalization.getRB().getString("yes"));
                            System.out.println(LoadInternationalization.getRB().getString("no"));
                            opt = sc.nextInt();
                        }
                        if (opt == 1){
                            //playCard(c);
                            played = true;
                        }
                    }
                }
            }
            if (played){
                playCard(uc);
            }
            if (!played){
                for (int i = 0; i<currentGame.getCardsToTake(); i++){
                    hand.add(currentGame.getFirstOnDeck());
                }
                currentGame.setRobado(true);
                System.out.println(LoadInternationalization.getRB().getString("take")+currentGame.getCardsToTake() +  LoadInternationalization.getRB().getString("cards"));
                currentGame.setCardsToTake(0);
            }
        }
        else if (currentGame.getTopCard().getRank().equals(UnoCard.Number.TAKEFOUR) && !currentGame.getRobado()){
            for (UnoCard c : hand){
                if (!played){
                    if (c.getRank().equals(UnoCard.Number.TAKEFOUR)){
                        while (opt != 1 && opt != 2){
                            System.out.println(LoadInternationalization.getRB().getString("anotherFOUR"));
                            System.out.println(LoadInternationalization.getRB().getString("yes"));
                            System.out.println(LoadInternationalization.getRB().getString("no"));
                            opt = sc.nextInt();
                        }
                        if (opt == 1){
                            //playCard(c);
                            played = true;
                        }
                    }
                }
            }
            if (played){
                playCard(uc);
            }
            if (!played){
                for (int i = 0; i<currentGame.getCardsToTake(); i++){
                    hand.add(currentGame.getFirstOnDeck());
                }
                currentGame.setRobado(true);
                System.out.println(LoadInternationalization.getRB().getString("take")+currentGame.getCardsToTake() +  LoadInternationalization.getRB().getString("cards"));
                currentGame.setCardsToTake(0);
            }
        }
        else{
            Collections.sort(hand);
            boolean terminado = false;
            int option;
            System.out.println(LoadInternationalization.getRB().getString("turn"));
            System.out.println(LoadInternationalization.getRB().getString("topOfStack"));
            System.out.println(currentGame.getTopCard());
            System.out.println(LoadInternationalization.getRB().getString("yourHand"));
            System.out.println(hand);
            while (!terminado){
                System.out.println(LoadInternationalization.getRB().getString("chooseOption"));
                System.out.println(LoadInternationalization.getRB().getString("checkHand"));
                System.out.println(LoadInternationalization.getRB().getString("playCard"));
                System.out.println(LoadInternationalization.getRB().getString("drawCard"));
                option = sc.nextInt();
                switch (option){
                    case 1:
                        checkHand();
                        break;
                    case 2:
                        if (chooseCardToPlay()){
                            terminado = true;
                        }
                        else{
                            System.out.println(LoadInternationalization.getRB().getString("invalidCard"));
                        }
                        break;
                    case 3:
                        if (shouldDraw()){
                            if (drawCard()){
                                System.out.println(LoadInternationalization.getRB().getString("drawnOne") + currentGame.getTopCard() + LoadInternationalization.getRB().getString("played"));
                            }
                            else {
                                System.out.println(LoadInternationalization.getRB().getString("drawnTWO"));
                            }
                            terminado = true;
                        }
                        else{
                            System.out.println(LoadInternationalization.getRB().getString("playIt"));
                        }
                        break;
                    default:
                        System.out.println(LoadInternationalization.getRB().getString("invalidOption"));
                        break;
                }
            }
            currentGame.setRobado(false);
        }
    }
    
     /**
     * Method to check if a card can be played
     * @param card
     * @return true if the card can be played
     */
    protected boolean canPlay(UnoCard card){
        return card.isJoker() ||
                card.getRank().equals(currentGame.getTopCard().getRank()) ||
                card.getSuit().equals(currentGame.getTopCard().getSuit()) ||
                card.getSuit().equals(currentGame.getColour());
    }
    
      /**
     * Method to check if a card has been played
     * @param card
     * @return true if the card has been played
     */
    
    protected boolean playCard(UnoCard card){
        if (canPlay(card)){
            currentGame.setTopCard(card);
            hand.remove(card);
            return true;
        }
        else{
            return false;
        }
    }
    
     /**
     * Method to check if a card has been drawn
     * @return true if the card has been drawn
     */
    
    protected boolean drawCard(){
        UnoCard c;
        c = currentGame.getFirstOnDeck();
        if (playCard(c)){
            return true;
        }
        else{
            hand.add(c);
            hand.add(currentGame.getFirstOnDeck());
            return false;
        }
    }
     /**
     * Method to check a hand
     */
    
    protected void checkHand() {
        for (int i = 0; i<hand.size(); i++){
            System.out.println(hand.get(i));
        }
    }
    
     /**
     * Method to choose the card to play
     * @return true if the card is the next card to be played
     */
    
    protected boolean chooseCardToPlay(){
        Scanner sc = new Scanner(System.in);
        int card;
        System.out.println(LoadInternationalization.getRB().getString("playedCard") + this.currentGame.getTopCard());
        for (int i = 0; i<hand.size();i++){
            System.out.println(i + " " + hand.get(i));
        }
        card = sc.nextInt();
        if (card >=0 && card < hand.size()){
            return playCard(hand.get(card));
        }
        else{
            System.out.println(LoadInternationalization.getRB().getString("invalidCard"));
            return false;
        }
    }
    
    /**
     * Method to check if the player should draw
     * @return true if the played should draw
     */
    
    protected boolean shouldDraw(){
        boolean draw = true;
        for (UnoCard c : hand){
            if (canPlay(c)){
                draw = false;
            }
        }
        return draw;
    }
    
    
    /**
     * Method to check if the hand is empty
     * @return true if the hand is empty
     */
    
    public boolean handIsEmpty(){
        return hand.isEmpty();
    }
    
    /**
     * Method to change the colour 
     * @return colour
     */
    public UnoCard.Colour changeColour(){
        int colour;
        System.out.println(LoadInternationalization.getRB().getString("chooseColour"));
        System.out.println(LoadInternationalization.getRB().getString("chooseRed"));
        System.out.println(LoadInternationalization.getRB().getString("ChooseBlue"));
        System.out.println(LoadInternationalization.getRB().getString("chooseYellow"));
        System.out.println(LoadInternationalization.getRB().getString("chooseGreen"));
        Scanner sc = new Scanner(System.in);
        colour = sc.nextInt();
        while (colour < 1 && colour >=5){
            System.out.println(LoadInternationalization.getRB().getString("invalidColour"));
            colour = sc.nextInt();
        }
        return UnoCard.Colour.valueOf(colour);
    }
    
    /**
     * Method to get the points of a hand
     * @return acum
     */
    
    public int getPoints(){
        int acum = 0;
        for (UnoCard uc : hand){
            acum += uc.points();
        }
        return acum;
    }
    /**
     * Method to get the id partida
     * @return currentGame.getIdPartida()
     */
    
    public int getIdPartida(){
        return currentGame.getIdPartida();
    }
}
